<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="分组名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name" :rules="[{required: true, message: '请输入分组名称!'}]">
              <a-input placeholder="请输入分组名称" v-model="model.name"></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { postAction, putAction } from '@/api/manage'
export default {
  name: "SysCreateGroupForm",
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data () {
    return {
      model:{
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {},
      url: {
        add: "/sys/sysDepart/add",
        edit: "/sys/sysDepart/edit",
        queryById: "/sys/sysDepart/queryById",
        groupAdd: "/sys/merchantGroup/add",
      },
    }
  },
  computed: {
    formDisabled(){
      return this.disabled
    },
  },
  created () {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model));
  },
  methods: {
    add () {
      this.edit(this.modelDefault);
    },
    edit (record) {
      this.model = Object.assign({}, record);
      this.visible = true;
    },
    submitForm () {
      const that = this;
      // 触发表单验证
      that.$refs.form.validate(valid => {
        if (valid) {
          let httpurl = '';
          let Action;
          let params = Object.assign({}, that.model);
          if(!that.model.id){
            Action = postAction;
            httpurl+=that.url.groupAdd;
          }else{
            Action = putAction;
            httpurl+=that.url.edit;
          }
          that.confirmLoading = true;
          Action(httpurl,params).then((res)=>{
            if(res.success){
              that.$message.success(res.message);
              that.$emit('ok');
            }else{
              that.$message.warning(res.message);
            }
          }).finally(() => {
            that.confirmLoading = false;
          })
        }
      })
    }
  }
}
</script>